<template>
    <div class="login">
        <el-form :model="form" status-icon :rules="rules" ref="loginForm" label-width="60px" class="login-form">
            <h3 class="title">民宿预约系统后台</h3>
            <el-form-item label="账号" prop="username">
                <el-input type="text" placeholder="请输入账号" auto-complete="off" v-model="form.username">
                    <i slot="prefix" class="el-input__icon input-icon"></i>
                </el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" placeholder="请输入密码" auto-complete="off" v-model="form.password">
                    <i slot="prefix" class="el-input__icon input-icon"></i>
                </el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-input
                        v-model="form.code"
                        auto-complete="off"
                        placeholder="验证码"
                        style="width: 63%">
                    <i slot="prefix" class="el-input__icon input-icon"></i>
                </el-input>
                <div class="login-code">
                    <img :src="codeUrl" @click="getCode" class="login-code-img"/>
                </div>
            </el-form-item>
            <el-checkbox v-model="rememberMe" style="margin:0px 0px 25px 60px;">记住密码</el-checkbox>
            <el-form-item style="width:100%;">
                <el-button
                        :loading="loading"
                        size="medium"
                        type="primary"
                        style="width:100%;"
                        @click="onSubmit('loginForm')">
                    <span v-if="!loading">登 录</span>
                    <span v-else>登 录 中...</span>
                </el-button>
                <div style="float: right;" v-if="register">
                    <router-link class="link-type" :to="'/register'">立即注册</router-link>
                </div>
            </el-form-item>
        </el-form>

        <!--  底部  -->
        <div class="el-login-footer">
            <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
        </div>

    </div>

</template>

<script>
    export default {
        name: "Login",
        data(){
            return{
                loading:false,
                rememberMe:false,
                codeUrl: "",
                // 注册开关
                register: false,
                form: {username: '0000', password: '123456'},
                // 表单验证，需要在 el-form-item 元素中增加 prop 属性
                rules: {
                    username: [
                        { required: true,message: '账号不可为空',trigger: 'blur' }
                    ],
                    password: [
                        { required: true,message: '密码不可为空',trigger: 'blur' }
                    ]
                }
            }
        },
        created() {
            this.getCode();
        },
        methods:{
            onSubmit(formName){
                // 为表单绑定验证功能
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.loading = true;
                        this.$http.get('/captchaImage?code='+this.form.code+'&uuid='+this.form.uuid).then(res=>{
                            console.log(res);
                            if (res.data.code === 200){
                                this.$http.post("/login?username="+this.form.username+"&password="+this.form.password).then(resp => {
                                    if(resp.data.code === 200){
                                        localStorage.setItem("token",resp.data.data);
                                        this.$router.push("/main")
                                    }else {
                                        this.$message({
                                            showClose: true,
                                            message: resp.data.message,
                                            type: 'warning'
                                        });
                                    }
                                })
                            }else {
                                this.$message({
                                    showClose: true,
                                    message: res.data.message,
                                    type: 'warning'
                                });
                            }
                        })
                    }
                });
            },
            getCode() {
                this.loading = false;
                this.$http.post('/captchaImage').then(res => {
                    console.log(res)
                    this.codeUrl = "data:image/gif;base64," + res.data.data.img;
                    this.form.uuid = res.data.data.uuid;
                    console.log(this.codeUrl)
                    console.log(this.form.uuid)
                });
            },
            getCookie() {
            },
        },
    }
</script>

<style scoped>
    .login {
        height: calc(100vh);
        display: flex;
        justify-content: center;
        align-items: center;
        /*height: 100%;*/
        background-image: url("../assets/img/login.jpg");
        background-size: cover;
    }
    .title {
        margin: 0px auto 30px auto;
        text-align: center;
        color: #707070;
    }

    .login-form {
        border-radius: 6px;
        background: #ffffff;
        width: 400px;
        padding: 25px 25px 5px 25px;
    }
    .login-form .el-input {
        height: 38px;
    }

    .login-form .el-input input {
        height: 38px;
    }

    .login-form .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 2px;
    }
    .login-tip {
        font-size: 13px;
        text-align: center;
        color: #bfbfbf;
    }
    .login-code {
        width: 33%;
        height: 38px;
        float: right;
    }
    .login-code img {
        cursor: pointer;
        vertical-align: middle;
    }
    .el-login-footer {
        height: 40px;
        line-height: 40px;
        position: fixed;
        bottom: 0;
        width: 100%;
        text-align: center;
        color: #fff;
        font-family: Arial;
        font-size: 12px;
        letter-spacing: 1px;
    }
    .login-code-img {
        height: 38px;
    }
</style>
